package com.cmcc.pcap.packet.sctp.sctp;

import com.cmcc.packet.entity.SctpEntity;
import com.cmcc.pcap.packet.sctp.SctpConnection;
import com.cmcc.pcap.packet.sctp.SctpProtocolHandler;
import com.cmcc.pcap.packet.sctp.sctp.xdr.SctpXdrGenerate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.List;


public class SctpHandler extends SctpProtocolHandler<SctpEntity> {

    private static Logger logger = LogManager.getLogger(SctpHandler.class);
    private SctpXdrGenerate sxg;

    public SctpHandler() {
    	sxg =  new SctpXdrGenerate();
    }

    @Override
    public List<SctpEntity> handle(SctpConnection sctpConnection, String taskId) {
		
    	logger.debug("start execute sctp packet handler");
		logger.debug(new Date(sctpConnection.lastPacketTime));
		long pcapTimestamp = (sctpConnection.al.get(sctpConnection.al.size() - 1)).timestamp.getTime()*1000L;
		logger.debug(new Date(pcapTimestamp));

		/**
		 * SCTP Connection，去掉重复DATA报文和重复SACK报文
		 */
		sctpConnection.sctpConnectionPacketsDeal();
		logger.debug("this connection <"+sctpConnection.connectionKey+"> hava packets (initial)  : "+sctpConnection.packetsNumInitial);
		logger.debug("this connection <"+sctpConnection.connectionKey+"> hava packets (after retransmission merge)  : "+sctpConnection.packetsNumAfterRetransmissionDataMerge);


		List<SctpEntity> xdrSctpEntityList = this.sxg.generate(sctpConnection,taskId);
		logger.debug("this connection <" + sctpConnection.connectionKey + "> saved into database !");
		return xdrSctpEntityList;
		
	}
    	

}
